## Universidade Federal do Rio Grande do Sul - Instituto de Informatica
## Departamento de Informatica Teorica
## Modelos de Linguagem de Programacao - 2013/1
## Professor: Lucas Schnorr
## Alunos: Felipe Schimidt e Mario Gasparoni Junior
##
##
## 
## Script principal para execucao do jogo.
## Esse script segue recomendacoes de:
## http://www.pygame.org/docs/

import sys, pygame, commons
import tower, enemies, shooter, background
import ammo, score 
import spoon, spoonultra

"setup before the main loop of the game"

#Global Variables Initialization
screen = pygame.display.set_mode((commons.MAX_HEIGHT, commons.MAX_WIDTH))
clock = pygame.time.Clock()

#Game start/logic
pygame.init()
done = False

#'Controller' Objects Instanciation
towers = tower.Tower()
background = background.Background()
enemy = enemies.Enemy()
shooter = shooter.Shooter()
ammunition = ammo.Ammo()
score = score.Score()

#Objects Lists Initialization
fired_spoons = []
placed_towers = []

#screen.fill((0,0,0))


"  Main Loop "

while not done:

	pressed = pygame.key.get_pressed()
	clicked = pygame.mouse.get_pressed()
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			done = True
		#Add new ammo object into the fired object list
		if pressed[pygame.K_SPACE]:
			fired_spoons.append(spoon.Spoon(shooter.center_x(), shooter.center_y(), 'south'))
		if pressed[pygame.K_x] and commons.ULTRA_SPOONS > 0:
			fired_spoons.append(spoonultra.SpoonUltra(shooter.center_x(), shooter.center_y(), 'south'))
			commons.ULTRA_SPOONS -= 1
		if clicked[0] == True:
			placed_towers.append(tower.Tower(towers.x_pos, towers.y_pos, towers.orientation))

	if commons.PLAYER_LIFE == 0:
		background.game_over(screen)
	else:
		#Updates/creates the background
		background.create(screen)
		#Moves/creates the enemies
		enemy.move(screen)
		#Keeps track shooters movement (keyboard)
		shooter.track(screen)
		#Keeps track of tower movement (mouse)
		towers.track(screen)
		#Controls the tower shooting. It's before the towers update just to make the ammo above the towers
		towers.shoot_all(screen, placed_towers)
		#Updates and draws the placed towers in the scenario
		towers.update(screen, placed_towers)
		#Updates the position in the screen of the shooter throwed spoons
		ammunition.update(screen, fired_spoons)
		#Keep tracks of the game score
		score.track(screen)

	pygame.display.flip()
	clock.tick(60)
